package cn.fx.framework.security;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

/**
 * AESCipherUtil：对称式加密
 * 1、AES加密与解密：encrypt()、decrypt()。
 * 2、使用的key与iv要求：key推荐16位字符,iv必须16位字符。(key与iv可以用各种随机数字符生成工具实现。如：UUID.randomUUID())
 * @author fxcn
 * @since 2024-9-20
 */
public class AESCipherUtil {

    private static final String ALGORITHM = "AES";
    static final String ALGORITHM_MODE_PADDING = "AES/CBC/PKCS5Padding";


    /**
     * AES加密函数
     * @param plainText 原文
     * @param key 推荐16位字符
     * @param iv 必须16位字符
     * @return 密文
     * @throws Exception
     */
    public static String encrypt(String plainText, String key, String iv) throws Exception {
        return encrypt(plainText, new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), ALGORITHM), new IvParameterSpec(iv.getBytes(StandardCharsets.UTF_8)));
    }

    private static String encrypt(String plainText, SecretKey secretKey, IvParameterSpec ivParameterSpec) throws Exception {
        Cipher cipher = Cipher.getInstance(ALGORITHM_MODE_PADDING);
        cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivParameterSpec);

        byte[] encryptedBytes = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));
        return Base64.getEncoder().encodeToString(encryptedBytes);
    }

    /**
     * AES解密函数
     * @param encryptedText 密文
     * @param key 推荐16位字符
     * @param iv 必须16位字符
     * @return 原文
     * @throws Exception
     */
    public static String decrypt(String encryptedText, String key, String iv) throws Exception {
        return decrypt(encryptedText, new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), ALGORITHM), new IvParameterSpec(iv.getBytes(StandardCharsets.UTF_8)));
    }

    private static String decrypt(String encryptedText, SecretKey secretKey, IvParameterSpec ivParameterSpec) throws Exception {
        Cipher cipher = Cipher.getInstance(ALGORITHM_MODE_PADDING);
        cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec);

        byte[] decodedBytes = Base64.getDecoder().decode(encryptedText);
        byte[] decryptedBytes = cipher.doFinal(decodedBytes);
        return new String(decryptedBytes,StandardCharsets.UTF_8);
    }

    public static void main(String[] args) throws Exception {
/*
        // 设置key与iv
        String key = "JUzfMGmV8JkvPpAX"; //推荐16位字符
        String iv = "HlFpk09DXI6wxgoX"; //必须16位字符
        System.out.println("key："+key+"，iv："+iv);

        // 原始文本
        String originalText = "Hello, AES Encryption!";
        System.out.println("原文："+originalText);
        // 加密
        String encryptedText = AESCipherUtil.encrypt(originalText, key, iv);
        System.out.println("Encrypted Text: " + encryptedText);
        // 解密
        String decryptedText = AESCipherUtil.decrypt(encryptedText, key, iv);
        System.out.println("Decrypted Text: " + decryptedText);
        */
    }
}
